<?php
/**
 * Dillenm-Mvc-Framework
 * 
 * A PHP MVC Framework written by Dillen Meijboom, all rights belong to him.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 * @url http://code.google.com/p/dillenm-mvc-framework/
 */

/**
 * Check the length of the value
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 */

class Dmf_Form_Validator_Length extends Dmf_Form_Validator
{
    protected $_message = null;
    
    public function isValid( $value )
    {
        $length = str_replace( ' ', '', $this->_value );
        
        if ( strstr( $length, '<' ) )
        {
            $parts = explode( '<', $length );
            $this->_message = 'Length must be lower then ' . $parts['1'];
            
            return ( strlen( $value ) < $parts['1'] );
        }
        
        if ( strstr( $length, '>' ) )
        {
            $parts = explode( '>', $length );
            $this->_message = 'Length must be higher then ' . $parts['1'];
            
            return ( strlen( $value ) > $parts['1'] );
        }
        
        if ( strstr( $length, '-' ) )
        {
            $parts = explode( '-', $length );
            $this->_message = 'Length must be between ' . $parts['0'] . ' and ' . $parts['1'] . ' characters';
            
            return ( strlen( $value ) >= $parts['0'] && strlen( $value ) <= $parts['1'] );
        }
        
        $this->_message = 'Length must be exactly ' . $this->_value . ' characters';
        
        return ( strlen( $value ) == $this->_value );
    }
}